package com.tj.jinzao.activity;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tj.jinzao.App;
import com.tj.jinzao.R;
import com.tj.jinzao.api.ApiRetrofit;
import com.tj.jinzao.api.ApiService;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.ExitBind;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.MessageLiveComment;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.model.User;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.widgets.ChooseConfigDialog;
import com.tj.jinzao.widgets.ChooseLiveUserDialog;

import java.util.HashMap;

import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * 账号管理页面
 * Created by wjy on 2019/1/5.
 */
public class AccountManagerActivity extends BaseActivity {
    @BindView(R.id.tv_phone)
    TextView tv_phone;
    @BindView(R.id.tv_weixin)
    TextView txt;
    private User user;
    private IWXAPI api;

    @Override
    public int getLayoutResource() {
        return R.layout.activity_account_manager;
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(user.getOpenid()!=null&&!user.getOpenid().equals("")){
            txt.setText("已绑定");
            txt.setTextColor(Color.parseColor("#2D2D49"));
        }else {
            txt.setText("未绑定");
            txt.setTextColor(Color.parseColor("#B9BDD2"));
        }

    }
    private ChooseConfigDialog mDialog;
    private void dialog(String content){
        mDialog = new ChooseConfigDialog(AccountManagerActivity.this, content);
        mDialog.show();
        mDialog.setOnClickLinstener(new ChooseConfigDialog.OnClickLinstener() {
            @Override
            public void onConfig(String mContent) {
                if(mContent.equals("1")){
                    getExitBind();
                }
                mDialog.dismiss();

            }
        });
    }

    @Override
    protected void setupView() {
        super.setupView();
        user= UserManager.getInstance().getUser();
        api = WXAPIFactory.createWXAPI(AccountManagerActivity.this, Constant.WX_APPID);
        api.registerApp(Constant.WX_APPID);

        if(user.getMobile()!=null){
            tv_phone.setText("+86 "+user.getMobile());
        }else {
            tv_phone.setText("");
        }

        txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(txt.getText())&&txt.getText().toString().equals("已绑定")){
                    dialog("确定取消绑定微信账号？");


                }else {
                    App.type_bind=1;
                    SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wx_login_state";
                    api.sendReq(req);
                }
            }
        });

    }
    protected ApiService mApiService = ApiRetrofit.getInstance().getApiService();

    private CompositeSubscription mCompositeSubscription;


    public void detachView() {
        onUnsubscribe();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        detachView();
        App.type_bind=0;
    }
    public void addSubscription(Observable observable, Subscriber subscriber) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeSubscription();
        }
        mCompositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber));
    }

    //RXjava取消注册，以避免内存泄露
    public void onUnsubscribe() {
        if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

    private void getExitBind(){
        startProgressDialog(this);
        HashMap<String,Object> map=new HashMap<>();
        map.put("uid",UserManager.getInstance().getUser().getId());
        map.put("type","weixin");
        addSubscription(mApiService.getExitBind(map), new SubscriberCallBack<ExitBind>(AccountManagerActivity.this) {
            @Override
            protected void onSuccess(ExitBind exitBind) {
                stopProgressDialog();
                user.setOpenid("");
                UserManager.saveUserData(user);
                txt.setText("未绑定");
                txt.setTextColor(Color.parseColor("#B9BDD2"));
            }

            @Override
            protected void onFailure(ResultResponse response) {
                user.setOpenid("");
                UserManager.saveUserData(user);
                txt.setText("未绑定");
                txt.setTextColor(Color.parseColor("#B9BDD2"));
                stopProgressDialog();
                ToastUtils.showToast(AccountManagerActivity.this,response.getMessage());
            }
            @Override
            protected void onStringNull(ResultResponse response) {

            }
        });

    }

}
